#! /usr/bin/python

def printMatrix(matrix):
    for i in range(9):
	print matrix[i]
    print "Grid"

def countZero(matrix):
    count = 1
    for i in range(9):
	for j in range(9):
	    if matrix[i][j] == 0: count += 1
    return count

def getProximate(matrix):
    full = set([1,2,3,4,5,6,7,8,9])  
    matrixcol = []
    diffcol = []
    for i in range(9):
	matrixcol.append([])
	for j in range(9):
	    matrixcol[i].append(matrix[j][i])
        diffcol.append(full - set(matrixcol[i]))

    proximateMatrix = []
    allEmpty = 0
    for i in range(9):
	diffrow = full - set(matrix[i])
	intersection = []
	for j in range(9):
	    if matrix[i][j] == 0:
		t = diffcol[j] & diffrow
		if len(t) == 0: allEmpty += 1
		intersection.append(diffcol[j] & diffrow)
	    else: 
		intersection.append(set())
		allEmpty += 1
	proximateMatrix.append(intersection)
    if allEmpty == 80: return []    
    return proximateMatrix

def checkMatrix(prox, matrix):
    for i in range(9):
	for j in range(9):
	    if matrix[i][j] == 0 and len(prox[i][j]) == 0:
		return False
    else: return True

def updateProximate(prox, a, row, col):
    r = prox[row]
    for j in range(9):
	if a in prox[row][j]:
	   prox[row][j].remove(a)
	if a in prox[j][col]:
	   prox[j][col].remove(a)
    return prox

def copy(prox):
    pre = []
    for i in range(9):
	pre.append([])
	for j in range(9):
	    s = set()
	    for a in prox[i][j]:
		s.add(a)
	    pre[i].append(s)
    return pre

def topLeftCorner(matrix):
    proximateMatrix = getProximate(matrix)
    if checkMatrix(proximateMatrix, matrix) == False:
	return 0
    printMatrix(proximateMatrix)
    #printMatrix(matrix)
    if len(proximateMatrix) == 0: 
	return matrix[0][0]*100 + matrix[0][1]*10 + matrix[0][2]
    else: 
      for i in range(9):
	for j in range(9):	    
	    if len(proximateMatrix[i][j]) != 0:
	       matrix[i][j] = proximateMatrix[i][j].pop()
               topLeftCorner(matrix)
	    else: continue
      return 0
    
f = open("sudoku.txt", "r")

order = 0
s = 0
while(order < 1):
     line = f.readline()
     matrix = []
     for i in range(9):
	matrix.append([])
	line = list(f.readline())
	for j in range(9):
	    matrix[i].append(int(line[j]))
     s += topLeftCorner(matrix)
     order += 1

f.close()

print s
